/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "image_data.h"

namespace ImageKnifePro {
    ImageData::ImageData(OH_PixelmapNative *pixelmap) : pixelmapList_(new OH_PixelmapNative *[1]), frameCount_(1)
    {
        pixelmapList_[0] = pixelmap;
    }

    ImageData::ImageData(OH_PixelmapNative **pixelmapList, int *delayTimeList, size_t frameCount)
        : pixelmapList_(pixelmapList), frameCount_(frameCount)
    {
        if (delayTimeList == nullptr) {
            return;
        }

        auto deleter = [](void *data) {
            int *timeList = static_cast<int *>(data);
            delete [] timeList;
        };
        delayTimeList_ = std::shared_ptr<int []>(delayTimeList, deleter);
    }

    ImageData::ImageData(OH_PixelmapNative **pixelmapList, std::shared_ptr<int []>delayTimeList, size_t frameCount)
        : pixelmapList_(pixelmapList), delayTimeList_(delayTimeList), frameCount_(frameCount)
    {
    }

    OH_PixelmapNative *ImageData::GetPixelmap() const
    {
        if (pixelmapList_ == nullptr) {
            return nullptr;
        }
        return pixelmapList_[0];
    }

    OH_PixelmapNative **ImageData::GetPixelmapList() const
    {
        return pixelmapList_;
    }

    int *ImageData::GetDelayTimeList() const
    {
        if (delayTimeList_ != nullptr) {
            return delayTimeList_.get();
        }
        return nullptr;
    }

    uint32_t ImageData::GetFrameCount() const
    {
        return frameCount_;
    }

    ImageData::~ImageData()
    {
        if (pixelmapList_ != nullptr) {
            for (int i = 0; i < frameCount_; i++) {
                if (pixelmapList_[i] != nullptr) {
                    OH_PixelmapNative_Release(pixelmapList_[i]);
                }
            }

            delete [] pixelmapList_;
        }
    }
}